﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using database_entry;
using database_constants;

public partial class WebUserControl : System.Web.UI.UserControl
{
    #region Globals

    private DBQuery DB_QUERY;
    private DBEntry DB_ENTRY;
    private DateTime sT, eT;

    #endregion
    protected void Page_Init(object sender, EventArgs e)
    {
        DB_ENTRY = new DBEntry();
        sT = new DateTime();
        eT = new DateTime();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        tpDemo2.LowerBoundTime = tpDemo1.SelectedTime;
    }

    #region Submit

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (validateData())
        {
            buildEntry();
            SqlConnection conn = new SqlConnection(Constants.DB_CONNECTION);
            try
            {
                conn.Open();
            }
            catch (Exception ex) { System.Console.WriteLine("Error connecting to SQL Database: " + ex.ToString()); }
            SqlCommand command = DB_ENTRY.InsertItem();
            command.Connection = conn;
            System.Console.WriteLine(command.ExecuteNonQuery());
            Button1.Text = "Add Event";
            Button1.ForeColor = System.Drawing.Color.Black;
        }
        else
        {
            Button1.Text = "Event not added!\nPlease enter all necessary data";
            Button1.ForeColor = System.Drawing.Color.Red;
        }
    }

    private bool validateData()
    {
        return (RequiredFieldValidator1.IsValid && 
            RequiredFieldValidator2.IsValid &&
            RequiredFieldValidator3.IsValid && 
            RequiredFieldValidator4.IsValid);
    }
    private void buildEntry()
    {
        DB_ENTRY.ClientID = int.Parse(cIDTextBox.Text);
        DB_ENTRY.Note = NoteTextBox.Text;
        DB_ENTRY.StartTime = new SqlDateTime(tpDemo1.SelectedTime);
        DB_ENTRY.EndTime = new SqlDateTime(tpDemo2.SelectedTime);
        DB_ENTRY.StartDate = new SqlDateTime(StartCal.SelectedDate);
        DB_ENTRY.EndDate = new SqlDateTime(EndCal.SelectedDate);
        DB_ENTRY.daily = CheckBox4.Checked;
        DB_ENTRY.weekly = CheckBox5.Checked;
        DB_ENTRY.monthly = CheckBox6.Checked;
        DB_ENTRY.mon = CheckBoxList2.Items[0].Selected;
        DB_ENTRY.tue = CheckBoxList2.Items[1].Selected;
        DB_ENTRY.wed = CheckBoxList2.Items[2].Selected;
        DB_ENTRY.thu = CheckBoxList2.Items[3].Selected;
        DB_ENTRY.fri = CheckBoxList2.Items[4].Selected;
        DB_ENTRY.sat = CheckBoxList2.Items[5].Selected;
        DB_ENTRY.sun = CheckBoxList2.Items[6].Selected;
        DB_ENTRY.DaysOfMonth = TextBox9.Text;
    }

#endregion

    #region Input Changes

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        DB_ENTRY.ClientID = int.Parse(cIDTextBox.Text);
    }

    protected void NoteTextBox_TextChanged(object sender, EventArgs e)
    {
        DB_ENTRY.Note = NoteTextBox.Text;
    }

    protected void Calendar2_SelectionChanged(object sender, EventArgs e)
    {
        DB_ENTRY.StartDate = new SqlDateTime(StartCal.SelectedDate);
        if (!CheckBox4.Checked && !CheckBox5.Checked && !CheckBox6.Checked)
            EndCal.SelectedDate = StartCal.SelectedDate;
    }

    protected void Calendar3_SelectionChanged(object sender, EventArgs e)
    {
        DB_ENTRY.EndDate = new SqlDateTime(EndCal.SelectedDate);
    }

    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {
        DB_ENTRY.daily = CheckBox4.Checked;
    }

    protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
    {
        DB_ENTRY.weekly = CheckBox5.Checked;
    }

    protected void CheckBox6_CheckedChanged(object sender, EventArgs e)
    {
        DB_ENTRY.monthly = CheckBox6.Checked;
    }

    protected void CheckBoxList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DB_ENTRY.mon = CheckBoxList2.Items[0].Selected;
        DB_ENTRY.tue = CheckBoxList2.Items[1].Selected;
        DB_ENTRY.wed = CheckBoxList2.Items[2].Selected;
        DB_ENTRY.thu = CheckBoxList2.Items[3].Selected;
        DB_ENTRY.fri = CheckBoxList2.Items[4].Selected;
        DB_ENTRY.sat = CheckBoxList2.Items[5].Selected;
        DB_ENTRY.sun = CheckBoxList2.Items[6].Selected;
    }

    protected void TextBox9_TextChanged(object sender, EventArgs e)
    {
        DB_ENTRY.DaysOfMonth = TextBox9.Text;
    }
    protected void StartTimeChanged(object sender, EventArgs e){
        tpDemo2.LowerBoundTime = tpDemo1.SelectedTime;
    }

    protected void EndTimeChanged(object sender, EventArgs e){}

    #endregion
}
